import { t } from 'i18next'
import { fetchCheckAuthentication, fetchSetCookie } from '@/api/global'
import Alert from '@/components/Alert'
import { MEETING_JUMP_URL } from '@/utils/constant'
import { useRef, useState } from 'react'

export interface CheckAuthenticationProps {
	onlyDialog?: boolean //会控以外使用，直接验证动态口令是否正确，即只利用弹窗
}
/*
const {
	run: checkAuthentication,
	open: isAuthenticationOpen,
	setOpen: setIsAuthenticationOpen,
	successFn,
} = useCheckAuthentication()
useCheckAuthentication 函数使用的时候，没有传入参数


const {
	run: checkAuthentication,
	open: isAuthenticationOpen,
	setOpen: setIsAuthenticationOpen,
	successFn,
} = useCheckAuthentication({ onlyDialog: true })
useCheckAuthentication 函数使用的时候，传入参数{ onlyDialog: true }

*/
// useCheckAuthentication 检查身份验证
export default function useCheckAuthentication(props?: CheckAuthenticationProps) {
	/*
	id 是 meetingMoid
	*/
	const id = useRef('')
	const [open, setOpen] = useState(false)
	/*
	cb 是 callback
	*/
	const cb = useRef<null | ((val: boolean) => void)>(null)
	const toMcc = () => {
		fetchSetCookie(
			{
				meeting_moid: id.current,
				type: 1,
			},
			() => {
				window.location.href = MEETING_JUMP_URL + '/mcc/manager/' + id.current
			}
		)
	}
	const showModal = () => {
		setOpen(true)
	}
	const successFn = (authenticationPassed: boolean) => {
		if (cb.current) {
			cb.current(authenticationPassed)
		} else {
			if (authenticationPassed) {
				toMcc()
			} else {
				if (
					(window.location.href.indexOf('/meeting/mcc') === -1 &&
						window.location.href.indexOf('meeting/meetingList') === -1) ||
					(window.location.href.indexOf('/index.html') === -1 &&
						window.location.href.indexOf('/meetingList.html') === -1)
				) {
					window.location.href = './index.html'
				}
			}
		}
	}
	const run = (meetingMoid: string, callback?: (val: boolean) => void) => {
		id.current = meetingMoid
		cb.current = callback ?? null
		/*
		
		*/
		if (props?.onlyDialog) {
			/*
			1.
			const {
				run: checkAuthentication,
				open: isAuthenticationOpen,
				setOpen: setIsAuthenticationOpen,
				successFn,
			} = useCheckAuthentication({ onlyDialog: true })

			showModal()

			---------------------------------------

			AuthenticationCheck 
			请输入动态口令 弹窗
			<AuthenticationCheck
				open={isAuthenticationOpen}
				setOpen={setIsAuthenticationOpen}
				callBack={successFn}
			/>

			*/
			showModal()
		} else {
			/*
			 * 2.api接口---校验是否开启终端强认证
			 * 与src\components\AuthenticationCheck组件连用
			 */
			fetchCheckAuthentication(meetingMoid).then((res) => {
				if (res.success) {
					if (!res.data) {
						/*
						3.不开启认证，直接出现弹窗  showModal
						*/
						showModal()
					} else {
						/*
						4. 开启认证，
						*/
						if (cb.current) {
							/*
							callback

							confirmModal 您确定要结束该会议吗？
							onOk fetchEndMeeting 接口

							*/
							cb.current(true)
						} else {
							toMcc()
						}
					}
				} else {
					Alert(res.description ?? t('操作失败'))
				}
			})
		}
	}
	return { run, open, setOpen, successFn }
}
